package com.googlecode.monitoring;

import com.googlecode.monitoring.config.SpringApplicationContext;
import com.googlecode.monitoring.service.CronTriggerFactory;
import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.springframework.scheduling.quartz.JobDetailBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.Map;

public class SystemRunner {

	private static Logger log = Logger.getLogger(SystemRunner.class);

	public static void run() {
		try {			
			SchedulerFactoryBean scheduler = SpringApplicationContext.getBean(SchedulerFactoryBean.class);
			CronTriggerFactory cronTriggerFactory = SpringApplicationContext.getBean(CronTriggerFactory.class);
			Map<JobDetailBean, CronTrigger> map = cronTriggerFactory.buildCronTriggerBeanMap();
			for (Map.Entry<JobDetailBean, CronTrigger> entry : map.entrySet()) {
				scheduler.getScheduler().scheduleJob(entry.getKey(), entry.getValue());
			}
			scheduler.start();
			log.debug("scheduler started...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
